package wt.hpad.model.holder;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.daimajia.swipe.implments.SwipeItemMangerImpl;

import wt.hpad.ConstantFields;
import wt.hpad.R;
import wt.hpad.base.BaseHolder;
import wt.hpad.base.BaseRvAdapter;
import wt.hpad.model.bean.Dish;
import wt.hpad.model.bean.DishDetail;
import wt.hpad.utils.proj.AnimUtil;
import wt.hpad.utils.proj.IAnimHandler;

import static wt.hpad.ConstantFields.CART_CURRENT;
import static wt.hpad.ConstantFields.ORDERVIEW_ENDVIEW;
import static wt.hpad.ConstantFields.ORDERVIEW_PARENTVIEWGROUP;

/**
 * Created by wt on 1.3.
 */

public class DishOrderHolder extends BaseHolder<Dish> {

    private TextView tv_dishName;
    private ImageView img_select;
    private ViewGroup mParent;
    private Context mContext;


    public DishOrderHolder(Context context, ViewGroup parent, BaseRvAdapter<Dish> adapter, int layoutID) {
        super(context, parent, adapter, layoutID);
        mContext = context;
        mParent = parent;
    }

    @Override
    public void setItemView(View itemView) {
        tv_dishName = (TextView) itemView.findViewById(R.id.tv_order_dishname);
        img_select = (ImageView) itemView.findViewById(R.id.img_select);
    }

    @Override
    public void bindItemValue(final Dish bean) {

        tv_dishName.setText(bean.dishName);
        // 添加购物车
        img_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理购物车
                addDishToCart(bean);
                AnimUtil.addPathAnimationByView(img_select,
                        ORDERVIEW_PARENTVIEWGROUP,
                        ORDERVIEW_ENDVIEW,
                        R.drawable.selected, mContext, new IAnimHandler() {
                            @Override
                            public void animEnd() {
                                // 处理 "购物车"控件 所显示的内容(商品数量)
                                ConstantFields.ORDERVIEW_CART_NUM.setText(String.valueOf(CART_CURRENT.size()));
                            }
                        });
            }
        });
    }

    private void addDishToCart(Dish dish) {
        // 购物车已有商品只修改其数量
        for (DishDetail item : CART_CURRENT) {
            if (item.dishID == dish.dishID) {
                item.dishNum++;
                return;
            }
        }
        // 添加购物车没有的商品
        CART_CURRENT.add(new DishDetail(dish.dishID, 1, dish.dishName, dish.dishTypeCode));
    }

    @Override
    public void bindSwItemValue(SwipeItemMangerImpl mItemManger, Dish bean, int position) {
    }
}
